package com.yanqu.road.dao.impl.activity.climbtower;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.climbtower.*;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ClimbTowerConfigDaoImpl extends TempDao {

    private static String getClimbTowerEventInfoSql = "select * from `t_s_activity_tongtian_event` where `activityId`=? ORDER BY `id` ASC";
    public List<ClimbTowerEventInfo> getClimbTowerEventInfoList(int activityId) {
        List<ClimbTowerEventInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getClimbTowerEventInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerEventInfo climbTowerEventInfo = new ClimbTowerEventInfo();
                climbTowerEventInfo.setActivityId(rs.getInt("activityId"));
                climbTowerEventInfo.setId(rs.getInt("id"));
                climbTowerEventInfo.setType(rs.getInt("type"));
                climbTowerEventInfo.setLittleType(rs.getInt("littleType"));
                climbTowerEventInfo.setExParam(rs.getString("exParam"));
                list.add(climbTowerEventInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    private static String getClimbTowerLevelInfoSql = "select * from `t_s_activity_tongtian_level` where `activityId`=? ORDER BY `id` ASC";
    public List<ClimbTowerLevelInfo> getClimbTowerLevelInfoList(int activityId) {
        List<ClimbTowerLevelInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getClimbTowerLevelInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerLevelInfo climbTowerLevelInfo = new ClimbTowerLevelInfo();
                climbTowerLevelInfo.setActivityId(rs.getInt("activityId"));
                climbTowerLevelInfo.setId(rs.getInt("id"));
                climbTowerLevelInfo.setBossId(rs.getInt("bossID"));
                climbTowerLevelInfo.setTemplateId(rs.getInt("templateId"));
                climbTowerLevelInfo.setReward(rs.getString("reward"));
                list.add(climbTowerLevelInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    private static String getClimbTowerMultipleInfoSql = "select * from `t_s_activity_tongtian_multiple` where `activityId`=? ORDER BY `id` ASC";
    public List<ClimbTowerMultipleInfo> getClimbTowerMultipleInfoList(int activityId) {
        List<ClimbTowerMultipleInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getClimbTowerMultipleInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerMultipleInfo data = new ClimbTowerMultipleInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setParam(rs.getString("param"));
                data.setValue(rs.getInt("value"));
                data.setWeight(rs.getInt("weight"));
                data.setElementNumList(StringUtils.stringToIntegerList(rs.getString("elementNum"), "\\|"));
                data.setElementWeightList(StringUtils.stringToStringList(rs.getString("elementWeight"), "\\|"));
                data.setIsNotice(rs.getInt("isNotic"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getClimbTowerRoleInfoSql = "select * from `t_s_activity_tongtian_role` where `activityId`=? ORDER BY `id` ASC";
    public List<ClimbTowerRoleInfo> getClimbTowerRoleInfoList(int activityId) {
        List<ClimbTowerRoleInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getClimbTowerRoleInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerRoleInfo climbTowerRoleInfo = new ClimbTowerRoleInfo();
                climbTowerRoleInfo.setActivityId(rs.getInt("activityId"));
                climbTowerRoleInfo.setId(rs.getInt("id"));
                climbTowerRoleInfo.setLevel(rs.getInt("level"));
                climbTowerRoleInfo.setExp(new BigInteger(rs.getString("exp")));
                climbTowerRoleInfo.setAddEffect(rs.getString("addEffect"));
                climbTowerRoleInfo.setPic(rs.getInt("pic"));
                climbTowerRoleInfo.setArmsPic(rs.getInt("armPic"));
                list.add(climbTowerRoleInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getClimbTowerTemplateInfoSql = "select * from `t_s_activity_tongtian_template` where `activityId`=? ORDER BY `id` ASC";
    public List<ClimbTowerTemplateInfo> getClimbTowerTemplateInfoList(int activityId) {
        List<ClimbTowerTemplateInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getClimbTowerTemplateInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerTemplateInfo climbTowerTemplateInfo = new ClimbTowerTemplateInfo();
                climbTowerTemplateInfo.setActivityId(rs.getInt("activityId"));
                climbTowerTemplateInfo.setId(rs.getInt("id"));
                climbTowerTemplateInfo.setType(rs.getInt("type"));
                climbTowerTemplateInfo.setCodeA(rs.getString("codeA"));
                climbTowerTemplateInfo.setEventList(StringUtils.stringToStringList(rs.getString("event"), "\\|"));
                climbTowerTemplateInfo.setPicList(StringUtils.stringToStringList(rs.getString("pic"), "\\|"));
                list.add(climbTowerTemplateInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getClimbTowerInfoSql = "select * from `t_s_activity_tongtian_tower` where `activityId`=? ORDER BY `id` ASC";
    public List<ClimbTowerInfo> getClimbTowerInfoList(int activityId) {
        List<ClimbTowerInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getClimbTowerInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerInfo climbTowerInfo = new ClimbTowerInfo();
                climbTowerInfo.setActivityId(rs.getInt("activityId"));
                climbTowerInfo.setId(rs.getInt("id"));
                climbTowerInfo.setAbility(rs.getString("ability"));
                list.add(climbTowerInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getClimbTowerTopInfoSql = "select * from `t_s_activity_tongtian_top` where `activityId`=? ORDER BY `cellNum` ASC";
    public List<ClimbTowerTopInfo> getClimbTowerTopInfoList(int activityId) {
        List<ClimbTowerTopInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getClimbTowerTopInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerTopInfo info = new ClimbTowerTopInfo();
                info.setActivityId(rs.getInt("activityId"));
                info.setCellNum(rs.getInt("cellNum"));
                info.setAbility(new BigInteger(rs.getString("ability")));
                info.setPic(rs.getInt("pic"));
                info.setReward(rs.getString("reward"));
                list.add(info);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
